<?php

namespace App\Policies;

use App\Models\AcceptRecord;
use App\Models\Customer;
use App\Models\Group;
use App\Models\Project;
use App\Models\User;
use Helper;
use Illuminate\Auth\Access\HandlesAuthorization;

class CustomerPolicy
{
    use HandlesAuthorization;

    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * 列表
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function index(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'role');
    }

    /**
     * 新增
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function create(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'role.create');
    }

    /**
     * 插入
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function store(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'customer.store');
    }

    /**
     * 详情
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function show(User $_user, Customer $_customer)
    {
        //return Helper::isActions($_user, 'customer.show');
        $bool = Helper::isActions($_user, 'customer.show');
        if (!$bool) {
            return false;
        }
        // 检查当前用户对于指定客户的权限
        $url = request()->url();
        $url_arr = explode('/', $url);
        $count = count($url_arr);
        if ($count < 3) {
            return true;
        }
        $id = $url_arr[count($url_arr) - 2];
        $customer = Customer::find($id);
        if (is_null($customer)) {
            return true;
        }
        $cus_project_ids = $customer->projects;
        // 客户项目为空, 说明是脏数据
        if (empty($cus_project_ids)) {
            return false;
        }
        $user_id = $_user->_id;
        $project = Project::where('users', $user_id)->first();
        // 为空 说明是上层账号
        if (is_null($project)) {
            $group_id = $_user->group_id;
            $group = new Group();
            $groups= $group->getLeafBy($group_id);
            $group_ids = array_column($groups, '_id');
            $project_ids = [];
            // 循环组织架构id, 找到对应的项目
            foreach ($group_ids as $g) {
                $pro = Project::where('group_id', $g)->first();
                if (!is_null($pro)) {
                    array_push($project_ids, $pro->_id);
                }
            }
            // 取数组交集
            $same = array_intersect($cus_project_ids, $project_ids);
            // 为空, 客户所属项目,不在此用户下
            if (empty($same)) {
                return false;
            } else {
                return true;
            }
        } else {
            // 负责人账号  得判断是否是主管
            $isManager = $_user->isManager;
            $user_pro_id = $project->_id;
            // 用户负责的项目, 不在客户的projects数组中 没有权限
            if (!in_array($user_pro_id, $cus_project_ids)) {
                return false;
            }
            if ($isManager) {
                return true;
                
            } else {
                return AcceptRecord::isAcceptByUser($id, $user_id);
            }
        }
    }

    /**
     * 编辑
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function edit(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'customer.edit');
    }

    /**
     * 修改
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function update(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'customer.update');
    }

    /**
     * 删除
     * @param User $_user
     * @param Customer $_customer
     * @return bool
     */
    public function destroy(User $_user, Customer $_customer)
    {
        return Helper::isActions($_user, 'customer.destroy');
    }

}
